﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using WebApiV3.Data;
using WebApiV3.InterFace.Log;

namespace WebApiV3.Utils.Filter
{
    public class GlobalExceptionFilter : IExceptionFilter
    {
        private readonly ILog _log;

        public GlobalExceptionFilter(ILog log)
        {
            _log = log;
        }
        public void OnException(ExceptionContext context)
        {
            _log.Error(context.Exception);
            if (context.ExceptionHandled == false)
            {
                context.Result = new ContentResult
                {
                    Content = context.Exception.Message,//这里是把异常抛出。也可以不抛出。
                    StatusCode = StatusCodes.Status200OK,
                    ContentType = "text/html;charset=utf-8"
                };
            }

            context.ExceptionHandled = true;
        }
    }

}
